﻿using System;
using System.Configuration;

namespace TrackIT.Common
{
    public abstract class TrackITBaseConfigurationSection : ConfigurationSection
    {
        public abstract String TaskFilename { get; set; }

        public abstract String MantisIssueNumberRegEx { get; set; }

        public abstract String MantisServer { get; set; }

        public abstract String ProjectId { get; set; }

        public abstract String UserName { get; set; }

        public abstract String Password { get; set; }

        public abstract String WorkFilename { get; set; }

        public abstract String LinePrefix { get; set; }

        public abstract String TaskPrefix { get; set; }

        public abstract String WorkPath { get; }

        public abstract String WorkFileNameWithoutPath { get; }

        public abstract bool DetectWorkFileChange { get; set; }

        public abstract bool UseMantis { get; set; }
    }

    public class TrackITConfigurationSection : TrackITBaseConfigurationSection
    {
        private const string configSectionName = "taskConfig";

        public void Save()
        {
            TrackITConfigurationSection customSection = GetSection();

            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            if (config.Sections[configSectionName] == null)
            {
                config.Sections.Add(configSectionName, customSection);
            }

            config.Save(ConfigurationSaveMode.Full);
        }

        public override bool IsReadOnly()
        {
            return false;
        }
        
        public static TrackITConfigurationSection GetSection()
        {
            try
            {
                return (TrackITConfigurationSection)ConfigurationManager.GetSection(configSectionName);
            }
            catch
            {
                return null;
            }
        }

        [ConfigurationProperty("taskFilename", IsRequired = true)]
        public override String TaskFilename
        {
            get { return (String)this["taskFilename"]; }
            set { this["taskFilename"] = value; }
        }

        [ConfigurationProperty("mantisIssueNumberRegEx", DefaultValue = "^([0-9]{7})$", IsRequired = false)]
        public override String MantisIssueNumberRegEx
        {
            get { return (String)this["mantisIssueNumberRegEx"]; }
            set { this["mantisIssueNumberRegEx"] = value; }
        }

        [ConfigurationProperty("mantisServer", DefaultValue = "mantis.eurofins.local", IsRequired = false)]
        public override String MantisServer
        {
            get { return (String)this["mantisServer"]; }
            set { this["mantisServer"] = value; }
        }

        [ConfigurationProperty("projectId", IsRequired = true)]
        public override String ProjectId
        {
            get { return (String)this["projectId"]; }
            set { this["projectId"] = value; }
        }

        [ConfigurationProperty("userName", IsRequired = true)]
        public override String UserName
        {
            get { return (String)this["userName"]; }
            set { this["userName"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public override String Password
        {
            get
            {
                return PasswordManager.DecryptFromBase64String((String)this["password"]);
            }
            set
            {
                this["password"] = PasswordManager.EncryptToBase64String(value);
            }
        }

        [ConfigurationProperty("workFilename", IsRequired = true)]
        public override String WorkFilename
        {
            get
            {
                return (String)this["workFilename"];
            }
            set
            {
                this["workFilename"] = value;
            }
        }

        [ConfigurationProperty("linePrefix", IsRequired = false, DefaultValue = "% ")]
        public override String LinePrefix
        {
            get
            {
                return (String)this["linePrefix"];
            }
            set
            {
                this["linePrefix"] = value;
            }
        }

        [ConfigurationProperty("taskPrefix", IsRequired = false, DefaultValue = "EOL_")]
        public override String TaskPrefix
        {
            get
            {
                return (String)this["taskPrefix"] ?? "";
            }
            set
            {
                this["taskPrefix"] = value;
            }
        }

        [ConfigurationProperty("detectWorkFileChange", IsRequired = false, DefaultValue = true)]
        public override bool DetectWorkFileChange
        {
            get
            {
                return (bool)this["detectWorkFileChange"];
            }
            set
            {
                this["detectWorkFileChange"] = value;
            }
        }

        [ConfigurationProperty("useMantis", IsRequired = false, DefaultValue = true)]
        public override bool UseMantis
        {
            get
            {
                return (bool)this["useMantis"];
            }
            set
            {
                this["useMantis"] = value;
            }
        }

        public override String WorkFileNameWithoutPath
        {
            get
            {
                int idx = WorkFilename.LastIndexOf('\\') + 1;
                return WorkFilename.Substring(idx, WorkFilename.Length - idx);
            }
        }

        public override String WorkPath
        {
            get
            {
                int idx = WorkFilename.LastIndexOf('\\');
                return WorkFilename.Substring(0, idx);
            }
        }
    }
}